// pages/myCardClient/myCardClient.js
const app = getApp()
const HttpReq = require('@/utils/HttpReq.js')
Page({
    /**
     * 页面的初始数据
     */
    data: {
      patternUrl: "https://www.nuoyin.cc:8081/img/cardFace/jianshen.png",
      couponNum: 0,
      input:'',
      type: 4,  // 1计次卡，2计时卡，3储值卡，4积分卡
      tag: 1,
      inComputing: false,
      storeName: '鑫源洗车行',
      storePhoneNumber: '54545',
      show: false,
      cardClient: {
        storeId: 1657413650,
        cardName: '汽车精洗',
        type: 3,
        money: 10.00,
        times: 7,
        days: 999
      }
    },
    goConsumer(){
        console.log(this.data.cardClient.type);
      this.setData({
        type: this.data.cardClient.type,
        inComputing: true
      })
    },

    goConsumerjifen(){
      this.setData({
        type: 4,
        inComputing: true
      })
    },
    getCouponLen(couponList){
      if(couponList == null){
        this.setData({
          couponNum: 0
        })
        return
      }
      else if(couponList == ''){
        this.setData({
          couponNum: 0
        })
        return
      }
      else{
        var strList = couponList.split(',')
        this.setData({
          couponNum: strList.length
        })
      }
    },

    Confirm(){
      let that = this
      if(this.data.input == '' && this.data.type != 2){
        this.setData({
          inComputing: false
        })
        return
      }
      else{
        let tt = {
          cardClientId: this.data.cardClientId,
          storeId: this.data.cardClient.storeId,
          type: this.data.type,
        }
        if(this.data.type == 3){
          tt.money = this.data.input
        }else if(this.data.type == 1){
          tt.times = this.data.input
        }else if(this.data.type == 4){
          tt.jifen = this.data.input
        }
        console.log(tt)
        let url = app.globalData.hostIp + '/weixin/consumerCardByClient'
        HttpReq.httpsResquest(url, tt, 'POST').then(res=>{
          if(res.code == 1){
            that.setData({
              inComputing: false,
              input: '',
              cardClient: res.data
            })
            wx.showToast({
              title: '核卡成功',
              icon:'success',
              duration: 2000,
            })
          }
          else if(res.code == 0){
            wx.showToast({
              title: res.msg,
              icon:'error',
              duration: 2000,
            })
          }
          else{
            wx.showToast({
              title: '请求发送失败',
              icon:'error',
              duration: 1500,
            })
          }
        })
        
      }
    },
    

    Cancel(){
      this.setData({
        inComputing: false
      })
    },
    ConJishi() {

    },

    makePhoneCall() {
      const phoneNumber = this.data.storePhoneNumber; // 替换成要拨打的电话号码
      wx.makePhoneCall({
        phoneNumber: phoneNumber
      })
    },
    add(e){
      // console.log(e.currentTarget.dataset.item)
     let i = e.currentTarget.dataset.item
     console.log(i)
     if(this.addCheckInput(i)){
       this.setData({
         input: this.data.input + e.currentTarget.dataset.item
       })
     }
     // this.setData({
     //   input: this.data.input + e.currentTarget.dataset.item
     // })
   },
   ToCoupon(){
    if(this.data.couponNum == 0){
      return
    }
    else{ 
      wx.navigateTo({
        url: '/pages/coupon/index?cardClientId=' + this.data.cardClient.id,
      })
    }
   },
   delete(){
     this.setData({
       input: this.data.input.slice(0, -1)
     })
   },
   addCheckInput(e){
     if(this.data.input.length == 0 && e === '.'){
       return false
     }
     if(this.data.input.length == 10){
       return false
     }
     if(this.data.type == 3){
       // 最多输入两位小数
       if(this.data.input.includes('.') && e === '.'){
         console.log('已有小数点，不能再输了')
         return false
       }
       let matchResult = this.data.input.match(/\.\d+/);
       if (matchResult) {
         // 如果匹配结果存在，则获取小数点后的数字部分，并计算位数
         let decimalPart = matchResult[0].substring(1);
         let decimalPlaces = decimalPart.length;
         console.log('小数点后的位数为:', decimalPlaces);
         if(decimalPlaces == 2){
           return false
         }
         else{
           return true
         }
       } else {
         console.log('该字符串不包含小数点');
         return true
       }
     }
     else{
       // 其他卡不能输入小数
       if(e === '.'){
         return false
       }
       return true
     }
   },
  

    onOpen() {
      this.setData({ show: true });
    },

    onClose() {
      this.setData({ show: false });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.setData({
        cardClientId: options.cardClientId
        
      })
      const cardClientId = options.cardClientId
      var this_ = this
      if(cardClientId != null){
        var url1 = app.globalData.hostIp + '/weixin/cardClient/' + cardClientId
        // console.log(url1);
        HttpReq.httpsResquest(url1, null, 'GET').then(res=>{
          if(res.code == 1){
            // console.log('下面是卡的详细信息');
            // console.log(res.data);
            this.setData({
              cardClient: res.data,
              patternUrl: res.data.patternUrl
            })
            this.getCouponLen(res.data.couponList)
            if(res.data.storeId != null){
              var url2 = app.globalData.hostIp + '/weixin/store/' + res.data.storeId
              HttpReq.httpsResquest(url2, null, 'GET').then(res2=>{
                if(res2.code == 1){
                  this_.setData({
                    storeHeaderUrl: res.data.storeHeaderUrl,
                    storeName: res2.data.name,
                    storePhoneNumber: res2.data.phoneNumber
                  })
                }
              })
            }
          }
        })
      }
    },
      

    // if(res.data.storeId != null){
    //   var url2 = app.global.hostIp + '/store/' + cardClientId
    //   HttpReq.httpsResquest(url1, null, 'GET')
    
    // }

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      console.log("onshow执行了")
      
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
      wx.reLaunch({
        url: '/pages/index/index'
      })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})